package com.zx.zxblog.controller.admin;

import com.zx.zxblog.entity.dto.BlogDTO;
import com.zx.zxblog.entity.pojo.Blog;
import com.zx.zxblog.mapper.BlogMapper;

import com.zx.zxblog.service.BlogService;
import com.zx.zxblog.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;


import javax.validation.Valid;

/**
 * className=AddBlogController
 * package=com.zx.zxblog.controller.admin
 * description:
 *
 * @Author:SKEZhi
 * @Create:2024/4/6 - 11:44
 * @Version: v1.0
 */
@RequestMapping(value = {"/admin"})
@Controller
public class AddBlogController {

    @Autowired
    private BlogMapper blogMapper;
    @Autowired
    private BlogService blogService;

    @GetMapping(value = {""})
    public String admin(Model model){
        return "admin/addBlog";
    }

    @ResponseBody
    @RequestMapping(value="/add",method= RequestMethod.POST)
    public String add(@RequestParam("title") String title,
                      @RequestParam("content") String content,
                      @RequestParam("description") String description){
        Blog blog = new Blog();
        blog.setTitle(title);
        blog.setContent(content);
        blog.setDescription(description);
        blog.setCommentabled(false);
        blog.setAppreciation(false);
        blog.setPublished(false);
        blog.setRecommend(false);
        blog.setShareStatement(false);
        blog.setViews(0);
        blog.setCommentCount(0);
        blogMapper.insert(blog);
        return title+"添加成功";
    }

//    @GetMapping(value = {"/manageBlog"})
//    public String addBlog(Model model){
//        List<Blog> blogList = blogMapper.selectList(null);
//        model.addAttribute("blogList", blogList);
//        return "admin/manageBlog";
//    }

//    @ResponseBody
//    @DeleteMapping(value = {"/delBlog"})
//    public String delBlog(@RequestParam(("id")) long id){
//        System.out.println(id);
//        blogMapper.deleteById(id);
//        return "删除成功";
//    }

//    @ResponseBody
//    @GetMapping(value = {"/blog/{id}"})
//    public Blog blog(@PathVariable("id")Long id){
//        Blog blog = blogMapper.selectById(id);
//        return blog;
//    }
//
//    @ResponseBody
//    @RequestMapping(value="/modify",method= RequestMethod.POST)
//    public String modify(@RequestParam("blogId") Long blogId,
//                        @RequestParam("title") String title,
//                      @RequestParam("content") String content,
//                      @RequestParam("description") String description){
//        blogMapper.deleteById(blogId);
//        Blog blog = new Blog();
//        blog.setId(blogId);
//        blog.setTitle(title);
//        blog.setContent(content);
//        blog.setDescription(description);
//        blog.setCommentabled(false);
//        blog.setAppreciation(false);
//        blog.setPublished(false);
//        blog.setRecommend(false);
//        blog.setShareStatement(false);
//        blog.setViews(0);
//        blog.setCommentCount(0);
//        blogMapper.insert(blog);
//        return title+"添加成功";
//    }
    @ResponseBody
    @PostMapping("/blogs")
    public R save(@RequestBody @Valid BlogDTO blogDTO) {
        return blogService.saveBlog(blogDTO);
}


}
